package com.hn.kiss.modules.sys.controller;

import com.hn.kiss.config.common.CommonResult;
import com.hn.kiss.modules.sys.dto.RoleJurisdictionTreeDTO;
import com.hn.kiss.modules.sys.service.IRoleJurisdictionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;


@Api(tags = "SysRoleJurisdictionController", description = "角色权限管理")
@RestController
@RequestMapping("/sys/role-jurisdiction")
@Validated
public class RoleJurisdictionController {

    @Autowired
    IRoleJurisdictionService roleJurisdictionService;

    @ApiOperation("角色权限管理树形数据")
    @ApiImplicitParam(name = "roleCode", value = "角色代码", required = true, dataType = "String", paramType = "query")
    @GetMapping("/tree")
    public CommonResult<List<RoleJurisdictionTreeDTO>> tree(@NotBlank String roleCode){
        List<RoleJurisdictionTreeDTO> tree = roleJurisdictionService.tree(roleCode);
        return CommonResult.success(tree);
    }


    @ApiOperation("角色权限分配")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleCode", value = "角色代码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "juriIds", value = "权限id(多个以','隔开)", dataType = "String", paramType = "query")
    })
    @PutMapping("/allocation")
    public CommonResult allocation(@NotBlank String roleCode, String juriIds){
        String[] split = juriIds.split(",");
        boolean b = roleJurisdictionService.allocation(roleCode, split);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }



}
